package com.anycl.spring.web.controller;

import com.anycl.spring.web.entity.Spitter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/spitter")
@Slf4j
public class SpitterController {

    @GetMapping("/register")
    public String register() {
        return "register";
    }

    @PostMapping("/register")
    public String processRegister(Spitter spitter) {
        log.info(spitter.toString());
        return "redirect:/spitter/" + spitter.getUsername();
    }

    /**
     * Content-Type application/json
     * @param spitter
     * @return
     */
    @PostMapping("/register/json")
    public String processRegisterWithJson(@RequestBody Spitter spitter) {
        log.info(spitter.toString());
        return "redirect:/spitter/" + spitter.getUsername();
    }


    @GetMapping("/{username}")
    public String processProfile(@PathVariable String username, Model model) {
        log.info("username: {}", username);
        model.addAttribute("username", username);
        return "profile";
    }
}
